<?php
class TemplateParser {
	private $output;
	private $template_file;
	private $tags;
	private $cache_file;
	private $expire_time;
	
	// Constructor
	function __construct( $template_file = 'default_template.htm', &$tags = array(), $cache_file = false, $expire_time = 3600){
		$this->template_file = $template_file;
		$this->tags = &$tags;
		$this->cache_file = $cache_file;
		$this->expire_time = $expire_time;
	}

	// Parser de plantillas
	function parse_template( $template_file = NULL, &$tags = NULL) {
		$template_file = $template_file ? $template_file : $this->template_file;
		$tags = $tags ? $tags : $this->tags;
		
		( file_exists( $template_file)) ? $this->output = file_get_contents( $template_file) : die( 'Error: Archivo '.$template_file.' no encontrado');
		if( count( $tags) > 0) {
			foreach( $tags as $tag => $data) {
				$data = ( file_exists( $data)) ? $this->parse_file( $data) : $data;
				$this->output = str_replace( '{'.$tag.'}', $data, $this->output);
			}
		} else {
			die( 'Error: No hay etiquetas que remplazar');
		}
		return $this->output;
	}

	function parse_template_cache( $template_file = NULL, &$tags = NULL, $cache_file = NULL) {
		$template_file = $template_file ? $template_file : $this->template_file;
		$tags = $tags ? $tags : $this->tags;
		$cache_file = $cache_file ? $cache_file : $this->cache_file;

		if( !$this->output = $this->read_cache( $cache_file, $expire_time)){
			( file_exists( $template_file)) ? $this->output = file_get_contents( $template_file) : die( 'Error: Archivo '.$template_file.' no encontrado');
			if( count( $tags) > 0) {
				foreach( $tags as $tag => $data) {
					$data = ( file_exists( $data)) ? $this->parse_file( $data) : $data;
					$this->output = str_replace( '{'.$tag.'}', $data, $this->output);
				}
				$this->write_cache( $cache_file, $this->output);
			} else {
				die( 'Error: No hay etiquetas que remplazar');
			}	
		}
		return $this->output;
	}
	
	// Retorna la plantilla procesada
	function display() {
		return $this->output;
	}

	// Reemplaza la etiqueta con el contenido del archivo indicado por la etiqueta
	function parse_file( $file){
		ob_start();
		include( $file);
		$content = ob_get_contents();
		ob_end_clean();
		return $content;
	}

	// Sistema de cache
	function read_cache( $cache_file, $expire_time){
		if( file_exists( $cache_file) && filemtime( $cache_file) > ( time() - $expire_time)){
			return file_get_contents( $cache_file);
		}
		return false;
	}

	function write_cache( $cache_file, $content){
		$fp = fopen( $cache_file, 'w');
		fwrite( $fp,$content);
		fclose( $fp);
	}
}
?>


<?php
/*
// EJEMPLO DE USO
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>{title}</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<link rel="stylesheet" type="text/css" href="style.css" />
	</head>
	<body>
		<div id="header">{header}</div>
		<div id="navbar">{navbar}</div>
		<div id="leftcol">{leftcontent}</div>
		<div id="content">{maincontent}</div>
		<div id="rightcol">{rightcontent}</div>
		<div id="footer">{footer}</div>
	</body>
</html>

// --------------------------------
// include the class
require_once('template.php');

// instantiate a new template Parser object
$tp = &new TemplateParser('template.htm');

// define parameters for the class
$tags=array('title'=>'You are seeing the template parser class in action!','header'=>'header.php','navbar'=>'navigation bar.php','leftcontent'=>'leftcontent.php',
	'maincontent'=>'maincontent.php',
	'rightcontent'=>'rightcontent.php',
	'footer'=>'footer.php');

// parse template file
$tp->parse_template( $tags);

// display generated page
echo $tp->display();
/**/
?>